package com.university.report.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.university.report.common.utils.ExcelUtils;
import com.university.report.model.entity.UserAccount;
import com.university.report.model.entity.UserInfo;

import com.university.report.model.vo.UserInfoVO;
import com.university.report.user.mapper.UserAccountMapper;
import com.university.report.user.mapper.UserInfoMapper;
import com.university.report.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jinondo
 * @since 2021-06-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserService {

    @Autowired
    UserInfoMapper userInfoMapper;

    public static final int PAGE_MAX = 10;

    @Override
    public List<UserInfo> getUnReported() {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("report_status",0);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<UserInfoVO> getUnReportedVO() {
        return userInfoMapper.selectUserVOByRS0();
    }


    @Override
    public void reSetReportStatus() {
        userInfoMapper.setReportStatusTo0();
    }

    @Override
    public void exportUnReported(HttpServletResponse response) throws IOException {
        List<UserInfoVO> unReportedVO = getUnReportedVO();
        String fileName = new SimpleDateFormat("yyyy年MM月dd日").format(new Date())
                +  "_未打卡名单";
        ExcelUtils.exportExcel(response,unReportedVO, UserInfoVO.class,fileName);
    }

    @Override
    public Map<String,Object> getUserInfoAtCondition(
            Integer id, String username, Integer reportStatus,
            Integer currentPage) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        if (id!=null) {
            queryWrapper.eq("user_id",id);
        }
        if (StringUtils.isNotEmpty(username)) {
            queryWrapper.like("real_name",username);
        }
        if (reportStatus!=null) {
            queryWrapper.eq("report_status",reportStatus);
        }
        Page<UserInfo> page = baseMapper.selectPage(new Page<>(currentPage, PAGE_MAX), queryWrapper);
        // 获取总页数
        long totalPage = page.getPages();
        // 获取记录
        List<UserInfo> userInfos = page.getRecords();
        Map<String, Object> map = new HashMap<String, Object>(){{
            put("totalPage",totalPage);
            put("currentPage",currentPage);
            put("userInfos",userInfos);
        }};
        return map;
    }

    @Override
    public UserInfoVO getUserInfoVOById(Integer userId) {
        return userInfoMapper.selectByUserId(userId);
    }




}
